package com.yksj.healthtalk.ui.login;

import sfs2x.client.core.SFSEvent;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.socket.SmartControlClient;
import com.yksj.healthtalk.services.CoreService;
import com.yksj.healthtalk.ui.MainTabUI;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;

/**
 * 
 * 注册选择
 * @author origin
 *
 */
public class UserDoctorRegisteUI extends BaseFragmentActivity implements OnClickListener{
	
	private LodingFragmentDialog mDialog;
	private SmartControlClient mControlClient;
	
	@SuppressLint("HandlerLeak")
	final Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case 1://登录超时
				if(mControlClient.getLoginState() != 2){
					if(mDialog != null && mDialog.isShowing()){
						mDialog.dismissAllowingStateLoss();
						mDialog = null;
					}
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),"登录超时,请稍后重试!");
				}
				break;
			case 2://登录错误
				if(mDialog != null && mDialog.isShowing()){
					mDialog.dismissAllowingStateLoss();
					mDialog = null;
				}
				SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),msg.obj.toString());
				break;
			case 3://登录成功
				Intent intent = new Intent(UserDoctorRegisteUI.this,MainTabUI.class);
				intent.putExtra("isFromLogin",true);
				startActivity(intent);
				finish();
				if(mDialog != null && mDialog.isShowing()){
					mDialog.dismissAllowingStateLoss();
					mDialog = null;
				}
				break;
			}
		};
	};
	
	final BroadcastReceiver mReceiver = new BroadcastReceiver(){
		public void onReceive(android.content.Context context, Intent intent) {
			int state = intent.getIntExtra("state",-1);
			//登录成功
			if(state == 2){
				
			//登录加载个人资料完成	
			}else if(state == 0){
				//是否显示协议
				mHandler.removeMessages(1);
				boolean isShow = intent.getBooleanExtra("isShowProtocol",false);
				Message message = mHandler.obtainMessage();
				message.what = 3;
				message.obj = isShow;
				mHandler.sendMessage(message);
			//登录错误	
			}else if(intent.hasExtra("errormsg")){
				mHandler.removeMessages(1);
				Message message = mHandler.obtainMessage();
				message.what = 2;
				message.obj = intent.getStringExtra("errormsg");
				mHandler.sendMessage(message);
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.user_registe_select_ui);
		
		initUI();
		mControlClient = SmartControlClient.getControlClient();
	}
	
	private void initUI(){
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		TextView textView = (TextView)findViewById(R.id.duomei_num);
		TextView textView2 = (TextView)findViewById(R.id.textView1);
		textView.setText(getIntent().getStringExtra("duomeinum"));
		findViewById(R.id.jump_btn).setOnClickListener(this);
		findViewById(R.id.registe_doctor).setOnClickListener(this);
		textView2.setText("您的注册已经完成！\n如果您是医生，请点击\"医生注册\"完成注册申请。或在首页 \"医生注册\" 中申请 。");
	}
	
	@Override
	public void onClick(View v) {
		Intent intent;
		switch(v.getId()){
		case R.id.jump_btn:
			onLogin();
//			intent = new Intent(this,UserRegisteComplet.class);
//			intent.putExtras(getIntent().getExtras());
//			startActivity(intent);
			break;
		case R.id.registe_doctor:
			intent = new Intent(this,DoctorRegisteUI.class);
//			intent.putExtras(getIntent().getExtras());
			startActivity(intent);
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		IntentFilter filter = new IntentFilter();
		filter.addAction(SFSEvent.LOGIN);
		registerReceiver(mReceiver, filter);
		if(mDialog != null && mDialog.isShowing()){
			mDialog.dismiss();
		}
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		mHandler.removeMessages(1);
		unregisterReceiver(mReceiver);
	}
	
	private void onLogin(){
		if(HTalkApplication.getApplication().isNetWork()){
			mDialog = LodingFragmentDialog.showLodingDialog(getSupportFragmentManager(),"登录中...");
			mDialog.setCancelable(false);
			Intent intent = getIntent();
			String duomeinum = intent.getStringExtra("duomeinum");
			String passwd = intent.getStringExtra("passwd");
			mControlClient.setUserPassword(duomeinum, passwd);
			mControlClient.setLoginState(0);
			CoreService.actionLogin(this);
			mHandler.sendEmptyMessageDelayed(1,SmartControlClient.CONNECTION_TIMEOUT);
		}else{
			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),"网络不可用");
		}
	}
}
